<template>
  <div class="home-container">
    <ul
      @transitionend="switching = false"
      class="carousel-container"
      @wheel="handleWheel"
      :style="{ marginTop }"
      ref="container"
      v-loading='loading'
    >
      <li v-for="item in data" :key="item.id">
        <Carouselitem  :carousel="item"/>
      </li>
    </ul>
    <div v-show="index > 0" class="btns btn-up" @click="switchTo(index - 1)">
      <Icon type="arrowUp" />
    </div>
    <div
      v-show="index < data.length - 1"
      class="btns btn-down"
      @click="switchTo(index + 1)"
    >
      <Icon type="arrowDown" />
    </div>
    <ul class="indicator">
      <li
        v-for="(item, i) in data"
        :key="item.id"
        @click="switchTo(i)"
        :class="{ active: index === i }"
      ></li>
    </ul>
  </div>
</template>

<script>
import Carouselitem from "./Carouselitem.vue";
import Icon from "@/components/Icon";
import { getBanners } from "@/api/banner";
import { mapState } from 'vuex';
export default {
  components: {
    Carouselitem,
    Icon,
  },
  data() {
    return {
      index: 0, //第几个页面
      containerHeight: 0, // 容器宽高
      switching: false, // 是否正在滑动
    };
  },
  created() {
    this.$store.dispatch('banner/fetchBanner')
  },
  mounted() {
    this.containerHeight = this.$refs.container.clientHeight;
    window.addEventListener("resize", this.handleResize);
  },
  destroyed(){
    window.removeEventListener('resize', this.handleResize)
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px";
    },
    ...mapState('banner', ['loading','data'])
  },
  methods: {
    switchTo(i) {
      this.index = i;
    },
    handleWheel(e) {
      if (this.switching) {
        return;
      }
      if (e.deltaY > 30 && this.index < this.data.length - 1) {
        this.switching = true;
        this.index++;
      }
      if (e.deltaY < 30 && this.index > 0) {
        this.switching = true;
        this.index--;
      }
    },
    handleResize(){
          this.containerHeight = this.$refs.container.clientHeight;
    }
  },
};
</script>

<style lang="less" scoped>
@import "~@/styles/global.less";
@import "~@/styles/mixin.less";

.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  .carousel-container {
    width: 100%;
    height: 100%;
    transition: 0.5s; 
    position: absolute;
    li {
      width: 100%;
      height: 100%;
    }
  }
  .btns {
    position: absolute;
    left: 50%;
    font-size: 30px;
    @gap: 25px;
    color: @gray;
    cursor: pointer;
    transform: translateX(-50%);
    &.btn-up {
      top: @gap;
      animation: btn-jump 1s infinite;
    }
    &.btn-down {
      bottom: @gap;
      animation: btn-jump 1s infinite reverse;
    }
  }

  @jump: 5px;
  @keyframes btn-jump {
    from {
      transform: translateX(-50%) translateY(@jump);
    }
    to {
      transform: translateX(-50%) translateY(-@jump);
    }
  }

  .indicator {
    .self-center();
    transform: translateY(-50%);
    left: auto;
    right: 20px;
    li {
      width: 7px;
      height: 7px;
      border-radius: 50%;
      // background: @words;
      background-color: transparent;
      cursor: pointer;
      margin-bottom: 10px;
      border: 1px solid #fff;
      box-sizing: border-box;
      transition: 0.5s;
      &.active {
        background-color: #fff;
        box-shadow: 0 0 5px #fff;
      }
    }
  }
}
</style>